package com.york.utils.exception;

import com.york.utils.exception.result.R;
import com.york.utils.exception.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 统一异常处理器
 * @author yangboxiong
 */
@Slf4j
@RestControllerAdvice
public class UnifiedExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public R handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error();
    }

    /**
     * 特定异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public R handleException(BadSqlGrammarException e) {
        log.error(e.getMessage(), e);
        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }

    @ExceptionHandler(value = BusinessException.class)
    public R handleException(BusinessException e) {
        log.error(e.getMessage(), e);
        return R.error().message(e.getMessage()).code(e.getCode());
    }

    /**
     * Controller上一层相关异常
     */
    @ExceptionHandler({
            //找不到处理程序异常
            NoHandlerFoundException.class,
            //不支持 Http 请求方法异常
            HttpRequestMethodNotSupportedException.class,
            //不支持 Http 媒体类型异常
            HttpMediaTypeNotSupportedException.class,
            //缺少路径变量异常
            MissingPathVariableException.class,
            //缺少 Servlet 请求参数异常
            MissingServletRequestParameterException.class,
            //类型不匹配异常
            TypeMismatchException.class,
            //Http 消息不可读异常
            HttpMessageNotReadableException.class,
            //Http 消息不可写异常
            HttpMessageNotWritableException.class,
            //方法参数无效异常
            MethodArgumentNotValidException.class,
            //Http 媒体类型不可接受异常
            HttpMediaTypeNotAcceptableException.class,
            //Servlet 请求绑定异常
            ServletRequestBindingException.class,
            //不支持转换异常
            ConversionNotSupportedException.class,
            //缺少 Servlet 请求部分异常
            MissingServletRequestPartException.class,
            //异步请求超时异常
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }
}

